home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / essent / FIXES / CSeries.exe / issue99 / CPROG8-A.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-05  |  5.5 KB  |  174 lines

  1. /* CPROG8-A.CPP - the bare bones of a menu system
  2.  
  3. THIS VERSION OF THE FILE IS COMMENTED TO DEATH. SO THAT YOU CAN CLEARLY
  4. SEE HOW THE PROGRAM IS STRUCTURED, PROGRAM FILE CPROG8.CPP IS EXACTLY
  5. THE SAME CODE WITH NO EXPLANATORY TEXT
  6. */
  7.  
  8.  
  9. #include <stdio.h>    // One of these .H files is needed for each
  10. #include <conio.h>    // library function used
  11. #include <stdlib.h>
  12. #include <dos.h>
  13. #include <ctype.h>
  14.  
  15. #define BEEP sound(300); delay(500); nosound();
  16.  
  17. ////////////////////////////////////
  18. void pause(void)                  // Pause program until user hits a key
  19. {
  20.     printf("\nPress a key...\n");
  21.     while( kbhit() == 0 )      // While kbhit() doesn't report a key
  22.                   // has been pressed...
  23.         ;          // ...execute this empty statement
  24.  
  25.     (void)getch();          // getch() clears the unwanted character
  26.                   // from the keyboard buffer. Strictly
  27.                   // speaking, we need to specify what
  28.                   // happens to the returned value. The
  29.                   // preceding (void) tidies this up using
  30.                   // a feature called typecasting.
  31.                   // Unfortunately typecasting is probably
  32.                   // outside the scope of the series -
  33.                   // read up on it if you decide to take
  34.                   // up C or C++ in earnest. pause() will
  35.                   // work without the (void) - TCP seems
  36.                   // to be happy to forget the returned
  37.                   // value and not throw a wobbly.
  38. }
  39.  
  40. ////////////////////////////////////
  41. void showmenu(void)          // This function clears the screen and
  42. {                  // prints the menu text
  43.     clrscr();
  44.     printf("Press...\n");
  45.     printf("1 ... for menu item 1\n");
  46.     printf("2 ... for menu item 2\n");
  47.     printf("3 ... for menu item 3\n");
  48.     printf("\nQ ... to quit\n");
  49. }
  50.  
  51. ////////////////////////////////////
  52. void action1(void)          // Functions action1(), action2() and
  53. {                  // action3() are dummies that are called
  54.     clrscr();          // up in response to menu items 1, 2 and 3
  55.     printf("The program code for menu item 1\n");
  56.     pause();
  57. }
  58.  
  59. ////////////////////////////////////
  60. void action2(void)
  61. {
  62.     clrscr();
  63.     printf("The program code for menu item 2\n");
  64.     pause();
  65.  
  66. }
  67.  
  68. ////////////////////////////////////
  69. void action3(void)
  70. {
  71.     clrscr();
  72.     printf("The program code for menu item 3\n");
  73.     pause();
  74.  
  75. }
  76.  
  77. ////////////////////////////////////
  78. ////////////////////////////////////
  79. int main(void)              // This program returns an integer value
  80. {                  // that appears in the DOS ERRORLEVEL.
  81. int kpress;    // Stores the ASCII code for the key that the user pressed
  82.  
  83.     while( 1 )  // The entire program is a loop. Since a whole number
  84.             // always evaluates as true, this loop never ends unless
  85.             // something inside it causes the program to cease. This
  86.             // action is activated by the user pressing the Q key.
  87.  
  88.         {   // Opening brace for the instructions in the loop body
  89.         showmenu();    // Clear the scren & display the menu text
  90.         kpress=getch();    // Library function getch() waits for the
  91.                 // user to press a key and returns the ASCII
  92.                 // value of the typed character. This goes
  93.                 // into integer variable kpress.
  94.         kpress=toupper(kpress); // Convert it to upper case to avoid
  95.                 // later testing for 'q' or 'Q'. Toupper() is
  96.                 // a library function detailed in the help
  97.  
  98.         switch( kpress )    // Choose from alternative courses of
  99.                     // action according to value in kpress
  100.             {    // opening brace of the SWITCH block
  101.             case '1':    // Could write case 49: since the
  102.                     // ASCII value for 1 is 49. C++ allows
  103.                     // you to use the actual character
  104.                     // between '' marks and will read it
  105.                     // as if you'd typed the raw number.
  106.                 action1();    // Do the actions associated
  107.                         // with this menu item.
  108.                 break;    // Don't drop through to action2(); -
  109.                     // jump to the point immediately after
  110.                     // the closing brace of the SWITCH
  111.                     // block
  112.  
  113.             case '2':
  114.                 action2();
  115.                 break;
  116.  
  117.             case '3':
  118.                 action3();
  119.                 break;
  120.  
  121.             case 'Q':    // kpress was earlier forced to upper
  122.                     // case. Otherwise it would be legal
  123.                     // to have case 'q': on the next line
  124.                     // to provide two routes into this
  125.                     // section
  126.                 exit(0);    // Another library function -
  127.                     // terminates the program as if it
  128.                     // had hit the closing brace of main()
  129.                     // but, like return(), will pass a
  130.                     // value back. This appears in DOS's
  131.                     // ERRORLEVEL variable and can be used
  132.                     // to signal whether the program
  133.                     // terminated satisfactorily (0) or
  134.                     // some error occured (choose a
  135.                     // value that you then detail the
  136.                     // meaning of in your documentation).
  137.                     // alternatively, you can also use
  138.                     // ERRORLEVEL to pass back a specific
  139.                     // piece of data. The old getkey
  140.                     // program for batch file menus
  141.                     // is an example
  142.  
  143.             default:    // If a key other than 1, 2, 3 or Q
  144.                 BEEP    // was pressed, complain!
  145.             }    // Closing brace for SWITCH block
  146.         }    // Closing brace for the loop body - execution jumps
  147.             // back to while( 1 )
  148. }
  149.  
  150. /* Further note: This code sacrifices a little compactness for the sake of
  151. clarity. The variable kpress is not needed...
  152.  
  153.     ...
  154.     ...
  155.     showmenu();
  156.     switch( toupper( getch() ) )
  157.         {
  158.         ...
  159.         ...
  160.  
  161. This works just as well. C++ evaluates the SWITCH line from the inside out:
  162.  
  163.     * Execute getch()
  164.     * Make its return value the parameter to toupper()
  165.     * Make the return value from toupper() the basis of the SWITCH()
  166.  
  167. You would only need kpress if you wanted to save the keypress for use later
  168. in the program. You could then write this:
  169.  
  170.     switch( kpress=toupper( getch() ) )
  171.  
  172. ...the return value from toupper() not only gets placed in kpress, but the
  173. whole kpress= expression evaluates to that number.
  174. */